package jsonp;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Jsonp extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Jsonp() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		System.out.println(request.getParameter("action"));
		
		if (request.getParameter("action").equals("getPluginVer")){
			getPluginVer( request, response);
		}else if(request.getParameter("action").equals("getCameraInfo")){
			getCameraInfo( request,  response);
		}else if(request.getParameter("action").equals("setCamera")){
			response.setCharacterEncoding("utf-8");
			String setCamera = request.getParameter("jsonpcallback");
			String resolution_x=request.getParameter("resolution_x");
			String resolution_y=request.getParameter("resolution_y");
			String size=request.getParameter("size");
			String quliaty=request.getParameter("quliaty");
			String format=request.getParameter("format");
			
			System.out.println("resolution_x: "+resolution_x+",resolution_y: "+resolution_y+",size: "+size+",quliaty: "+quliaty+",format: "+format);
			
			String info="{\"code\":\"code\",\"msg\":\"msg\"}";
			info=setCamera+"("+info+")";
			System.out.println(info);
			response.getWriter().print(info);
		}else if(request.getParameter("action").equals("setHandSign")){
			response.setCharacterEncoding("utf-8");
			String setHandSign = request.getParameter("jsonpcallback");
			String size=request.getParameter("size");
			String format=request.getParameter("format");
			String position=request.getParameter("position");
			
			System.out.println("size: "+size+",format: "+format+",position: "+position);
			
			String info="{\"code\":\"code\",\"msg\":\"msg\"}";
			info=setHandSign+"("+info+")";
			System.out.println(info);
			response.getWriter().print(info);
		}
	}

	public void getPluginVer(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String ver = request.getParameter("jsonpcallback");
		String str = "{\"version\":\"123\",\"code\":\"123\",\"msg\":\"123\"}";
		str = ver + "("+ str +")";
		System.out.println(str);
		response.getWriter().print(str);
	}
	public void getCameraInfo(HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		String cameraInfo = request.getParameter("jsonpcallback");
		String info="{\"code\":\"code\",\"msg\":\"msg\",\"status\":\"status\",\"resolution_x\":\"resolution_x\",\"resolution_y\":\"resolution_y\",\"model\":\"model\"}";
		info=cameraInfo+"("+info+")";
		System.out.println(info);
		response.getWriter().print(info);
	}
	public void init() throws ServletException {
		// Put your code here
	}

}
